#include <iostream>
#include <cstdlib>
#include <string>
#include <cassert>
#include <vector>
#include <pthread.h>
#include <unistd.h>

using namespace std;

void *start_routine(void *args)
{
    // sleep(1);
    // 一个线程如果出现了异常，会影响其他线程吗？会的（健壮性或者鲁棒性较差）
    // 为什么？进程信号，信号是整体发给进程的！

    string name = static_cast<const char *>(args); // 安全的进行强制类型转化
    int cnt = 10;
    while (true)
    {

        cout << "new thread create success, name: " << name << endl;
        // int *p = nullptr;
        // // p = nullptr;
        // *p = 0;
    }
}

int main()
{
    // 1. 我们想创建一批线程
    vector<pthread_t> threads;
#define NUM 10
    for (int i = 0; i < NUM; i++)
    {
        pthread_t tid;
        char namebuffer[64];
        snprintf(namebuffer, sizeof(namebuffer), "%s:%d", "thread", i);
        // pthread_create(&tid, nullptr, start_routine, i);
        threads.push_back(tid);

        // pthread_create(&tid, nullptr, start_routine, (void*)"thread one");
        pthread_create(&tid, nullptr, start_routine, namebuffer);
        sleep(1);
    }
}
